const API = require('../../../../utils/api')
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        form:{},
        positionArr:['法人','股东','高管','人事','行政','普通员工'],
        positionArrIndex: 0,
        //picker数组
        postArr:['法人','股东','高管','人事','行政','普通员工'],
        postIndex: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            postArr: app.globalData.positionArr
        })
        this.get_card_detail( options.cardId )
    },

    /**
     * 事件- 职位选择
     */
    postArrChange:function(e){
        let that = this
        this.setData({
            postIndex: parseInt( e.detail.value ),
            ['form.post']: that.data.postArr[parseInt( e.detail.value )]
        })
    },

    /**
     * 接口 - 获取名片详情
     */
    get_card_detail:function(id){
        let that = this
        API.get_card_detail({
            id: id
        }).then( res => {
            if( res.code == 200 ){
                let data = res.data
                let form = {}
                form.id = data.id
                form.checkCardId = data.checkCardId
                form.companyProfile = data.companyProfile
                form.mailbox = data.mailbox
                form.name = data.name
                form.phone = data.phone
                form.portrait = data.portrait
                form.selfIntroduction = data.selfIntroduction
                form.sign = data.sign
                form.site = data.site
                form.wxYard = data.wxYard
                //找到职位在的位置
                form.post = data.post
                let postIndex = that.data.postArr.findIndex( item => item == data.post )
                if( postIndex == -1 ){
                    postIndex = 0
                    form.post = that.data.postArr[postIndex]
                }
                // console.log(postIndex, that.data.postArr)
                this.setData({
                    form,
                    postIndex
                })
            }else{
                wx.showToast({
                  title: res.msg,
                  icon:'none'
                })
            }
        })
    },

    /**
     * 微信二维码上传
     */
    uploadWxYard:function(){
        let that = this
        app.uploadImg( 1, callback ) 
        function callback(data){
            that.setData({
                ['form.wxYard']: data
            })
        } 
    },

    /**
     * 删除微信二维码
     */
    deleteWxYard:function(){
        this.setData({
            ['form.wxYard']: ''
        })
    },

    /**
     *头像上传
     */
    uploadPortrait:function(){
        let that = this
        app.uploadImg( 1, callback ) 
        function callback(data){
            that.setData({
                ['form.portrait']: data
            })
        } 
    },

    /**
     * 删除头像
     */
    deletePortrait:function(){
        this.setData({
            ['form.portrait']: ''
        })
    },

    /**
     * 表单提交
     */
    submit:function(e){
        let that = this
        console.log(e.detail.value)
        let formData = e.detail.value
        let form = this.data.form
        form.companyProfile = formData.companyProfile
        form.mailbox = formData.mailbox
        form.name = formData.name
        form.phone = formData.phone
        form.selfIntroduction = formData.selfIntroduction
        form.site = formData.site

        console.log(form)
        //表单拦截
        if( !form.name ){
            wx.showToast({
              title: '请输入姓名',
              icon:'none'
            })
            return
        }
        if( !form.phone ){
            wx.showToast({
              title: '请输入手机号',
              icon:'none'
            })
            return
        }
        if( !form.companyProfile ){
            wx.showToast({
              title: '请输入企业介绍',
              icon:'none'
            })
            return
        }
        if( !form.site ){
            wx.showToast({
              title: '请输入详细地址',
              icon:'none'
            })
            return
        }
        if( !form.selfIntroduction ){
            wx.showToast({
              title: '请输入个人简介',
              icon:'none'
            })
            return
        }
        if( !form.wxYard ){
            wx.showToast({
              title: '请上传微信二维码',
              icon:'none'
            })
            return
        }
        if( !form.portrait ){
            wx.showToast({
              title: '请上传个人照片',
              icon:'none'
            })
            return
        }

        API.update_card_detail( form ).then( res => {
            if( res.code == 200 ){
                wx.showToast({
                  title: '修改成功',
                })
                setTimeout( () => {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 1500)
            }else{
                wx.showToast({
                  title: res.msg,
                  icon:'none'
                })
            }
        })
    },

})